<template>
  <div>
    <a-modal
      :visible="visible"
      :title="title"
      @cancel="cancelChange"
      @ok="onSubmit"
      :confirmLoading="confirmLoading"
      :width="800"
      centered
    >
      <a-form layout="vertical">
        <a-row :gutter="[20, 0]">
          <a-col :span="8">
            <a-form-item label="联系人" v-bind="validateInfos.contact_name">
              <a-input
                v-model:value="modelRef.contact_name"
                placeholder="请输入联系人"
              />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="联系电话" v-bind="validateInfos.contact_phone">
              <a-input-number
                style="width: 100%"
                v-model:value="modelRef.contact_phone"
                :min="0"
                :precision="0"
                placeholder="请输入联系电话"
              />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="职务" v-bind="validateInfos.contact_job">
              <a-input
                v-model:value="modelRef.contact_job"
                placeholder="请输入职务"
              />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="公司" v-bind="validateInfos.contact_company">
              <a-input
                v-model:value="modelRef.contact_company"
                placeholder="请输入公司"
              />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="邮箱" v-bind="validateInfos.contact_email">
              <a-input
                v-model:value="modelRef.contact_email"
                placeholder="请输入邮箱"
              />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item
              label="通讯地址"
              v-bind="validateInfos.contact_address"
            >
              <a-input
                v-model:value="modelRef.contact_address"
                placeholder="请输入通讯地址"
              />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-modal>
  </div>
</template>

<script>
import { defineComponent, ref, toRefs, computed, watch, reactive } from "vue";
import request from "@/common/utils/request";
import { Form, message } from "ant-design-vue";
const useForm = Form.useForm;

export default defineComponent({
  name: "AddBrand",
  components: {},
  props: {
    visible: {
      type: Boolean,
      default: false,
    },
    id: {
      type: [String, Number],
      default: 0,
    },
    contactId: {
      type: [String, Number],
      default: 0,
    },
  },
  setup(props, context) {
    const state = reactive({
      count: 0,
      parentOptions: [],
      confirmLoading: false,
    });

    const modelRef = reactive({
      contact_name: "",
      contact_phone: "",
      contact_job: "",
      contact_company: "",
      contact_email: "",
      contact_address: "",
    });
    const rulesRef = reactive({
      contact_name: [
        {
          required: true,
          message: "请输入联系人",
        },
      ],
      contact_phone: [
        {
          required: true,
          message: "请输入联系电话",
        },
      ],
      contact_job: [
        {
          required: true,
          message: "请输入职务",
        },
      ],
      contact_company: [
        {
          required: true,
          message: "请输入公司",
        },
      ],
    });

    const { resetFields, validate, validateInfos } = useForm(
      modelRef,
      rulesRef
    );

    const cancelChange = () => {
      resetFields();
      context.emit("close");
    };

    const onSubmit = () => {
      validate().then(() => {
        state.confirmLoading = true;
        modelRef.brand_id = props.id;
        if (props.contactId) {
          request
            .put(
              "/org",
              `/org/brand/custom/brand/contact/${props.contactId}`,
              modelRef
            )
            .then((res) => {
              state.confirmLoading = false;
              message.success(res.msg || "修改成功");
              resetFields();
              context.emit("success");
            })
            .catch((err) => {
              state.confirmLoading = false;
            });
          return;
        }
        request
          .post("/org", "/org/brand/custom/brand/contact", modelRef)
          .then((res) => {
            state.confirmLoading = false;
            message.success(res.msg || "保存成功");
            resetFields();
            context.emit("success");
          })
          .catch((err) => {
            state.confirmLoading = false;
          });
      });
    };

    const title = computed(() => {
      return props.contactId ? "编辑联系人" : "新增联系人";
    });

    const getContactDetail = () => {
      request
        .get("/org", `/org/brand/custom/brand/contact/${props.contactId}`)
        .then((res) => {
          sendModelRef(res.detail);
        });
    };

    const sendModelRef = (data) => {
      for (let key in data) {
        if (modelRef.hasOwnProperty(key)) {
          modelRef[key] = data[key];
        }
      }
    };

    watch(
      () => props.visible,
      (val) => {
        if (val) {
          if (props.contactId) {
            getContactDetail();
          }
        }
      }
    );

    return {
      ...toRefs(state),
      title,
      onSubmit,
      cancelChange,
      validateInfos,
      modelRef,
    };
  },
});
</script>

<style lang="less" scoped></style>
